package lc.回溯;

import utils.TreeNode;

import java.util.LinkedList;
import java.util.List;

/**
 * @description:
 * @author: qian
 * @createDate: 2021/10/26
 */
public class 剑指Offer34_二叉树中和为某一值的路径 {
    LinkedList<List<Integer>> res = new LinkedList<>();
    LinkedList<Integer> path = new LinkedList<>();
    public List<List<Integer>> pathSum(TreeNode root, int target) {

        dfs(root,target);
        return res;
    }

    private void dfs(TreeNode node, int target) {

        if (node == null) return;
        path.add(node.val);
        target -= node.val;
        if (target == 0 && node.left == null && node.right == null) {
            res.add(new LinkedList<>(path));
        }
        dfs(node.left,target);
        dfs(node.right,target);
        // lc.回溯
        path.removeLast();
    }
}
